Deployment Status Apache License Documentation Status Python Online Python version—Jan 27, 2021


Copyright © Wei MEI, MLMS™—all rights reserved. 🀤

Handling conditions

Conditional execution can be completed using the if statement. Adding elif allows you to check multiple conditions

Boolean operators

  • x *or* y - If either x OR y is true, the expression is executed

Comparison operators

  • < less than
  • < greater than
  • == is equal to
  • >= greater than or equal to
  • <= less than or equal to
  • != not equal to
  • x *in* [a,b,c] Does x match the value of a, b, or c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
province = input("What province do you live in? ")
tax = 0
# If multiple values cause the same output you can combine them by listing all 
# values you want to check for with the in operator
if province in('Alberta','Nunavut','Yukon'):
	tax = 0.05
elif province == 'Ontario':
	tax = 0.13
else:
	tax = 0.15
print(tax)

Demo: dates

1
2
3
4
5
6
7
8
9
province = input("What province do you live in? ")
tax = 0
if province == 'Alberta': 
	tax = 0.05
elif province == 'Nunavut':
	tax = 0.05
elif province == 'Ontario':
	tax = 0.13
print(tax)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
province = input("What province do you live in? ")
tax = 0
if province == 'Alberta' \
   or province == 'Nunavut':
	tax = 0.05
elif province == 'Ontario':
	tax = 0.13
else:
	tax = 0.15
print(tax)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
country = input("What country do you live in? ")

if country.lower() == 'canada':
	province = input("What province/state do you live in? ")
	if province in('Alberta',\
       'Nunavut','Yukon'):
		tax = 0.05
	elif province == 'Ontario':
		tax = 0.13
	else:
		tax = 0.15
else:
	tax = 0.0
print(tax)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

province = input("What province do you live in? ")
tax = 0

if province == 'Alberta': 
	tax = 0.05
if province == 'Nunavut':
	tax = 0.05
if province == 'Ontario':
	tax = 0.13
print(tax)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
province = input("What province do you live in? ")
tax = 0

if province == 'Alberta':
	tax = 0.05
elif province == 'Nunavut':
	tax = 0.05
elif province == 'Ontario':
	tax = 0.13
else:
	tax = 0.15
print(tax)

PPT Demonstrations

Challenges time

Check the following script and try to find the mistake:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Ask a user their name
# If their first name starts with A or B 
# tell them they go to room AB
# IF their first name starts with C
# tell them to go to room CD
# If their first name starts with another letter, ask for their last name
# IF their last name starts with Z, tell them to go to room Z
# if their last name starts with any other letter, tell them to go to room OTHER
# When you are done
# Anna should be in room AB
# Bob should be in room AB
# Charlie should be in room C
# Khalid Haque should be in room OTHER
# Xin Zhao should be in room Z

solutions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Assign people to different rooms when they check in based on their names
# When you are done
# Anna should be in room AB
# Bob should be in room AB
# Charlie should be in room C
# Khalid Haque should be in room OTHER
# Xin Zhao should be in room Z
# Ask a user their first name
name = input('What is your name? ')

# If their first name starts with A or B
# tell them they go to room AB 
first_letter = name[0:1]
if first_letter.upper() in ('A','B'):
    room = 'AB'
# If their first name starts with C
# tell them to go to room C
elif first_letter.upper() == 'C':
    room = 'C'
else:
    # If their first name starts with another letter, ask for their last name
    # If their last name starts with Z, tell them to go to room Z
    last_name = input('what is your last name? ')
    last_name_first_letter = last_name[0:1]
    # if their last name starts with any other letter, tell them to go to room OTHER
    if last_name_first_letter == 'Z':
        room = 'Z'
    else:
        room = 'OTHER'
print('Please go to room ' + room)